import appConfigStoreModule from '@core/@app-config/appConfigStoreModule'
import Vue from 'vue'
import Vuex from 'vuex'
import app from './app'
import snackbar from './snackbar'
import i18n from './i18n'

//change dev to prod
import GlobalConfig from '@/config/global.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    ethAddress: '',
    ethChainID: '',
    web3: null,
    ethersprovider: null,
    walletConnectProvider: null,
    walletName: '',
    ethpublickey: '',
    networkError: false,
    GlobalConfig: GlobalConfig,
    overlay: false,
    snackbar: {
      isOpen: false,
      title: ''
    },
    contract: {
      /**
       * @SCT合约地址 --测试
       */
      contractAddress: '0xa4B068aD60766E20283C0424C1F543E45100cFE3',
      /**
       * @质押合约地址  --测试
       */
      pledgeContractAddress: '0xEC52e76571950da8dD20ecf1b065C6699164BD58'
    }

  },
  mutations: {
    changeContractAddress(state, payload) {
      state.contract = payload
    },
    changeSnackbar(state, payload) {
      state.snackbar = payload
    },
    changeOverlay(state, payload) {
      state.overlay = payload
    },
    changeEthAddress(state, address) {
      state.ethAddress = address
    },
    changeEthChainID(state, id) {
      state.ethChainID = id
    },
    changeweb3(state, obj) {
      state.web3 = obj.web3
      state.ethersprovider = obj.ethersprovider
    },
    changeWalletConnectProvider(state, ConnectProvider) {
      state.walletConnectProvider = ConnectProvider
    },
    walletName(state, name) {
      state.walletName = name
    },
    networkError(state, boolean) {
      state.networkError = boolean
    },
    ethpublickey(state, key) {
      state.ethpublickey = key
    }
  },
  actions: {
    async getEthChainID({ state, commit }) {
      try {
        const res = await state.web3.eth.getChainId()
        commit('changeEthChainID', res)
      } catch (error) {
        console.log(error)
      }
    }
  },
  modules: {
    appConfig: appConfigStoreModule,
    app,
    i18n,
    snackbar,
  },
})
